package app.fugema.shop.main.order.adapter;

import com.base.common.utils.DensityUtil;
import com.base.common.utils.UIUtils;
import com.base.common.utils.ViewUtils;
import com.base.common.utils.span.SpanUtils;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;
import com.base.common.view.widget.imageView.ImageLoaderUtils;

import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;

import app.fugema.shop.R;
import app.fugema.shop.databinding.GroupBookingHeadImageItemBinding;
import app.fugema.shop.databinding.ItemGroupOrderNeedPeopleViewBinding;
import app.fugema.shop.main.order.model.GroupPeopleHeadBean;

public class GroupOrderPeopleMultiType extends BaseItemMultiType<GroupPeopleHeadBean, ItemGroupOrderNeedPeopleViewBinding> {

    @Override
    public int getItemViewType()
    {
        return TYPE_HEAD_TWO;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.item_group_order_need_people_view;
    }

    @Override
    public void onCreateViewHolder(ItemGroupOrderNeedPeopleViewBinding binding, BaseViewHolder viewHolder)
    {
        super.onCreateViewHolder(binding, viewHolder);
        binding.rvImg.setLayoutManager(new MyLinearLayoutManager(viewHolder.itemView.getContext(), 0, false));
        binding.rvImg.setAdapter(new SingleRecyclerAdapter<String, GroupBookingHeadImageItemBinding>() {
            @Override
            public int getLayoutId() {
                return R.layout.group_booking_head_image_item;
            }

            @Override
            public void onBindViewHolder(GroupBookingHeadImageItemBinding binding, int position, String bean) {
                super.onBindViewHolder(binding, position, bean);
                ImageLoaderUtils.loadImage(binding.givHeadImage, bean);
                if (binding.givHeadImage.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                    ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) binding.givHeadImage.getLayoutParams();
                    if (position == 0) {
                        marginLayoutParams.leftMargin = DensityUtil.getDimens(R.dimen.dp_0);
                    } else {
                        marginLayoutParams.leftMargin = DensityUtil.getDimens(R.dimen.dp_f10);
                    }
                    binding.givHeadImage.setLayoutParams(marginLayoutParams);
                }

            }
        });
    }

    @Override
    public void onBindViewHolder(ItemGroupOrderNeedPeopleViewBinding binding, int position, BaseViewHolder viewHolder, GroupPeopleHeadBean bean)
    {
        super.onBindViewHolder(binding, position, viewHolder, bean);
        ViewUtils.setViewMarginTop(binding.clPeopleView,DensityUtil.getDimens(R.dimen.dp_12),DensityUtil.getDimens(R.dimen.dp_12));

        //判断 是否 够拼团人数
        if( bean.getLimitedNumber() - bean.getJoinedNumber() > 0 )
        {
            SpanUtils.with(binding.tvNeedPeopleTips)
                    .append("还差")
                    .append( String.valueOf( (bean.getLimitedNumber() - bean.getJoinedNumber()) ) )
                    .setClickSpan(new ClickableSpan() {

                        @Override
                        public void updateDrawState(TextPaint ds)
                        {
                            super.updateDrawState(ds);
                            ds.setColor(0xFFFE1439);
                            ds.setUnderlineText(false);
                        }

                        @Override
                        public void onClick(View widget)
                        {
                        }
                    })
                    .append("人拼团成功")
                    .create();
        }
        else
        {
            SpanUtils.with(binding.tvNeedPeopleTips)
                    .append("共")
                    .append(String.valueOf( bean.getLimitedNumber() ))
                    .setClickSpan(new ClickableSpan() {

                        @Override
                        public void updateDrawState(TextPaint ds)
                        {
                            super.updateDrawState(ds);
                            ds.setColor(0xFFFE1439);
                            ds.setUnderlineText(false);
                        }

                        @Override
                        public void onClick(View widget)
                        {
                        }
                    })
                    .append("人拼团成功！")
                    .create();
        }

        if (binding.rvImg.getAdapter() instanceof BaseRVAdapter)
        {
            BaseRVAdapter rvAdapter = (BaseRVAdapter) binding.rvImg.getAdapter();
            if (UIUtils.isNotEmpty(bean.getUserIcons()))
            {
                rvAdapter.setDataList(bean.getUserIcons());
            }
            else
            {
                rvAdapter.clear();
            }
        }

    }

    @Override
    public void onItemClick(View view, int onclickType, int position, GroupPeopleHeadBean bean)
    {
        super.onItemClick(view, onclickType, position, bean);
        //AddressManageActivity.start(view.getContext(), true);
    }
}
